package com.sunyard.client2.ui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import com.sunyard.client.SunEcmClientApi;
import com.sunyard.client.bean.ClientBatchBean;
import com.sunyard.client.bean.ClientBatchFileBean;
import com.sunyard.client2.ConnectionFactory;
import com.sunyard.client2.conf.ConfUtil;
import com.sunyard.client2.util.StringUtil;
import com.sunyard.util.DateUtil;

public class UpdateBatchInfo extends JPanel {
	private JTextField textField;
	private JTextField textField_1;
	private JTextField textField_2;
	private JTextField textField_3;
	private JLabel label_4;
	private JTextField textField_4;
	private JButton button;
	private JLabel label_6;
	private JLabel label_7;
	private JTextField textField3;
	private JTextField textField2;
	private JTextField textField22;
	private JTextField textField33;

	/**
	 * Create the panel.
	 */
	public UpdateBatchInfo() {
		setLayout(null);

		JLabel label = new JLabel("模型：");
		label.setHorizontalAlignment(SwingConstants.RIGHT);
		label.setBounds(78, 46, 76, 15);
		add(label);

		textField = new JTextField();
		textField.setText(ConfUtil.getString("modelCode", ""));
		textField.setColumns(10);
		textField.setBounds(164, 43, 389, 21);
		add(textField);

		JLabel label_1 = new JLabel("批次编号：");
		label_1.setHorizontalAlignment(SwingConstants.RIGHT);
		label_1.setBounds(78, 106, 76, 15);
		add(label_1);

		textField_1 = new JTextField();
		textField_1.setColumns(10);
		textField_1.setBounds(164, 103, 389, 21);
		add(textField_1);

		JLabel label_2 = new JLabel("时间字段：");
		label_2.setHorizontalAlignment(SwingConstants.RIGHT);
		label_2.setBounds(78, 151, 76, 15);
		add(label_2);

		textField_2 = new JTextField();
		textField_2.setText(ConfUtil.getString("STARTCOLUMN", ""));
		textField_2.setColumns(10);
		textField_2.setBounds(164, 148, 140, 21);
		add(textField_2);

		JLabel label_3 = new JLabel("业务时间：");
		label_3.setHorizontalAlignment(SwingConstants.RIGHT);
		label_3.setBounds(327, 151, 76, 15);
		add(label_3);

		textField_3 = new JTextField();
		textField_3.setText(ConfUtil.getString("STARTDATE", DateUtil.get8bitDateStr()));
		textField_3.setColumns(10);
		textField_3.setBounds(413, 148, 140, 21);
		add(textField_3);

		label_4 = new JLabel("文件模型：");
		label_4.setHorizontalAlignment(SwingConstants.RIGHT);
		label_4.setBounds(78, 78, 76, 15);
		add(label_4);

		textField_4 = new JTextField();
		textField_4.setText(ConfUtil.getString("filePartName", ""));
		textField_4.setColumns(10);
		textField_4.setBounds(164, 75, 389, 21);
		add(textField_4);

		button = new JButton("更新");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				ClientBatchBean bean = getBatchBean();
				if (StringUtil.isNotNull(textField2.getText()) && StringUtil.isNotNull(textField22.getText())) {
					bean.getIndex_Object().addCustomMap(textField2.getText(), textField22.getText());
				}

				if (StringUtil.isNotNull(textField3.getText()) && StringUtil.isNotNull(textField33.getText())) {
					bean.getIndex_Object().addCustomMap(textField3.getText(), textField33.getText());
				}
				bean.setDocument_Objects(null);
				try {
					SunEcmClientApi clientApi = ConnectionFactory.getConnection();
					String resultMsg = clientApi.update(bean, ConfUtil.getString("groupName", ""), true);
					ResultMessageBox.getInstance().showMessage(resultMsg);
				} catch (Exception e1) {
					e1.printStackTrace();
					ResultMessageBox.getInstance().showMessage(e1.getMessage());
				}
			}
		});
		button.setBounds(284, 280, 93, 23);
		add(button);

		label_6 = new JLabel("条件2:");
		label_6.setHorizontalAlignment(SwingConstants.RIGHT);
		label_6.setBounds(78, 192, 54, 15);
		add(label_6);

		label_7 = new JLabel("条件3:");
		label_7.setHorizontalAlignment(SwingConstants.RIGHT);
		label_7.setBounds(78, 225, 54, 15);
		add(label_7);

		textField3 = new JTextField();
		textField3.setText("");
		textField3.setColumns(10);
		textField3.setBounds(164, 225, 178, 21);
		add(textField3);

		textField2 = new JTextField();
		textField2.setText("");
		textField2.setColumns(10);
		textField2.setBounds(164, 192, 178, 21);
		add(textField2);

		textField22 = new JTextField();
		textField22.setColumns(10);
		textField22.setBounds(375, 192, 178, 21);
		add(textField22);

		textField33 = new JTextField();
		textField33.setColumns(10);
		textField33.setBounds(375, 225, 178, 21);
		add(textField33);
		textField2.setText(ConfUtil.getString("userAttr2", ""));
		textField3.setText(ConfUtil.getString("userAttr3", ""));

	}

	public ClientBatchBean getBatchBean() {
		ClientBatchBean clientBatchBean = new ClientBatchBean();
		clientBatchBean.setToken_code(ConfUtil.getString("token_value", ""));

		clientBatchBean.setModelCode(textField.getText());
		clientBatchBean.setUser(ConfUtil.getString("userName", ""));
		clientBatchBean.setPassWord(ConfUtil.getString("passWord", ""));
		clientBatchBean.setOwnMD5(true);
		clientBatchBean.getIndex_Object().setContentID(textField_1.getText());
		clientBatchBean.getIndex_Object().addCustomMap(textField_2.getText(), textField_3.getText());

		ClientBatchFileBean batchFileBean = new ClientBatchFileBean();
		batchFileBean.setFilePartName(textField_4.getText());
		clientBatchBean.addDocument_Object(batchFileBean);
		return clientBatchBean;
	}

}
